package cn.wolfcode.luowowo.service.impl;

import cn.wolfcode.luowowo.cache.key.RedisKey;
import cn.wolfcode.luowowo.cache.service.IQuestionCacheService;
import cn.wolfcode.luowowo.cache.vo.QuestionStats;
import com.alibaba.fastjson.JSON;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@Service
public class QuestionCacheServiceImpl implements IQuestionCacheService {
    @Autowired
    private StringRedisTemplate template;

    public void setQuestionStats(QuestionStats vo) {
        String cacheKey = RedisKey.QUESTION_STATS.getCacheKey(vo.getQId());
        //直接把提问的id作为key,因为提问的id是唯一的
        template.opsForValue().set(cacheKey, JSON.toJSONString(vo));
    }

    public QuestionStats getQuestionStats(Object id){
        String cacheKey = RedisKey.QUESTION_STATS.getCacheKey(id);
        String json = template.opsForValue().get(cacheKey);
        return   JSON.parseObject(json,QuestionStats.class);
    }


    public void incrNum(String id, int num, int type) {
        QuestionStats vo = getQuestionStats(id);
        if (vo != null) {
            switch (type) {
                case IQuestionCacheService.QUESTION_VIUMNUM:
                    vo.setViewnum(vo.getViewnum() + num);
                    break;
                case IQuestionCacheService.QUESTION_REPLYNUM:
                    vo.setReplynum(vo.getReplynum() + num);
                    break;
                case IQuestionCacheService.QUESTION_FAVORNUM:
                    vo.setFavornum(vo.getFavornum()+num);
                    break;
            }
            setQuestionStats(vo);
        }
    }

    public boolean getAttention(String qId, Long userId) {
        boolean flag=true;
        ValueOperations<String, String> op = template.opsForValue();
        //获取提问的所有关注在redis中的key
        String cacheKey = RedisKey.QUESTION_STATS_FAVOR.getCacheKey(qId);
        //user,所有关注的id
        List<Long> users = new ArrayList<>();
        //num:偏移值
        int num = 0;
        //判断是否有被关注过
        if (template.hasKey(cacheKey)){//有被关注
            //查询redis中的关注
            String vo = op.get(cacheKey);
            //将字符串数据解析成Long类型的数据
            users = JSON.parseArray(vo,Long.class);
            //判断users中是否已有当前登录用户
            if (users.contains(userId)){
                //取消关注
                users.remove(userId);
                num = -1;
                flag=false;
            }else {
                //关注
                users.add(userId);
                num = 1;
            }
        }else{//提问第一次被关注
            //将当前登录用户id加入list集合
            users.add(userId);
            num = 1;
        }
        //将users存进redis
        op.set(cacheKey,JSON.toJSONString(users));
        //更新关注数据
        incrNum(qId,num,QUESTION_FAVORNUM);
        return flag;
    }



    public Boolean hasKey(String qId) {
        String cacheKey = RedisKey.QUESTION_STATS.getCacheKey(qId);
        return template.hasKey(cacheKey);
    }

    public List<QuestionStats> listAllQuestion() {
        ValueOperations<String, String> op = template.opsForValue();
        List vos=new ArrayList();
        //拿出所有带QUESTION_STATS * 的key
        String cacheKey = RedisKey.QUESTION_STATS.getCacheKey("*");
        Set<String> keys = template.keys(cacheKey);
       //遍历key,得到vo值
        for (String key : keys) {
            String json = op.get(key);
            vos.add(JSON.parseObject(json,QuestionStats.class));
        }
        return vos;
    }

    public boolean exit(String id) {
        String cacheKey = RedisKey.QUESTION_STATS.getCacheKey(id);
        return template.hasKey(cacheKey);
    }

}
